//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormPrib.h"
#include "DataModule.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FR_Class"
#pragma link "FR_Ctrls"
#pragma link "FR_View"
#pragma link "FR_DBSet"
#pragma link "FR_DSet"
#pragma resource "*.dfm"
TFRepPrib *FRepPrib;
//---------------------------------------------------------------------------
__fastcall TFRepPrib::TFRepPrib(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::SpeedButton1Click(TObject *Sender)
{
 // Eanble
 frSpeedButton1->Enabled = true;
 frSpeedButton2->Enabled = true;
 frSpeedButton3->Enabled = true;
 frSpeedButton4->Enabled = true;
 frSpeedButton5->Enabled = true;
 frSpeedButton6->Enabled = true;
 frSpeedButton7->Enabled = true;
 frSpeedButton10->Enabled = true;
 // set Sqls for reports
 if(AskOutcome->Active)
  AskOutcome->Close();
 if(AskIncom->Active)
  AskIncom->Close();
 if(AskSum->Active)
  AskSum->Close();
 AskIncom->SQL->Text = "select S.SELLDATE, P.ART, P.NAME, SD.CNT, SD.PRICE, SD.INCOME  from SELL S INNER JOIN SELLDET SD ON SD.IDSELL = S.IDSELL INNER JOIN PRODUCT P ON P.IDPRODUCT = SD.IDPRODUCT where  S.SELLDATE <= '" +
  DateToStr(EndDate->Date) + "' and S.SELLDATE>='" + DateToStr(BegDate->Date) + "' ORDER BY S.SELLDATE";
 AskOutcome->SQL->Text = "Select OC.NAME, O.OUTDATE, O.PRICE from OUTCOME O INNER JOIN OUTCAT OC ON O.IDOUTCAT = OC.IDOUTCAT where  O.OUTDATE <= '" +
  DateToStr(EndDate->Date) + "' and O.OUTDATE>='" + DateToStr(BegDate->Date) + "' ORDER BY O.OUTDATE, O.IDOUTCAT";
 AskSum->SQL->Text = "select COALESCE((select sum(PRICE) from OUTCOME O1 where O1.OUTDATE <= '" +
  DateToStr(EndDate->Date) + "' and O1.OUTDATE>='" + DateToStr(BegDate->Date) + "'), 0) as O, COALESCE((select sum(INCOME) from SELLDET SD INNER JOIN SELL S ON SD.IDSELL = S.IDSELL where  S.SELLDATE <= '" +
  DateToStr(EndDate->Date) + "' and S.SELLDATE>='" + DateToStr(BegDate->Date) + "'), 0) as I from STUB";
 AskSum->Open();
 
 RepIn->PrepareReport();
 RepIn->ShowPreparedReport();
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton2Click(TObject *Sender)
{
 fP->Zoom = 100;
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton1Click(TObject *Sender)
{
 fP->OnePage();
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton3Click(TObject *Sender)
{
 fP->PageWidth();
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton4Click(TObject *Sender)
{
 fP->First();
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton5Click(TObject *Sender)
{
 fP->Prev();          
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton6Click(TObject *Sender)
{
 fP->Next();           
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton7Click(TObject *Sender)
{
 fP->Last();
}
//---------------------------------------------------------------------------
void __fastcall TFRepPrib::frSpeedButton10Click(TObject *Sender)
{
 fP->Print();            
}
//---------------------------------------------------------------------------

void __fastcall TFRepPrib::FormCreate(TObject *Sender)
{
 // set data to last month
 EndDate->Date = Date();
 BegDate->Date = IncMonth(Date(), -1);
}
//---------------------------------------------------------------------------

void __fastcall TFRepPrib::RepInUserFunction(const AnsiString Name,
      Variant &p1, Variant &p2, Variant &p3, Variant &Val)
{
 if(Name == "BEG")
 {
  Val = DateToStr(BegDate->Date);
 }
  if(Name == "END")
 {
  Val = DateToStr(EndDate->Date);
 }
}
//---------------------------------------------------------------------------

